//package health_system.health_system.config;
//
//import com.fasterxml.jackson.annotation.JsonInclude;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
//import health_system.health_system.websocket.SpringWebSocketHandler;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.messaging.simp.config.ChannelRegistration;
//import org.springframework.messaging.simp.config.MessageBrokerRegistry;
//import org.springframework.web.socket.config.annotation.*;
//import org.springframework.web.socket.server.standard.ServerEndpointExporter;
//
//import java.text.SimpleDateFormat;
//
///**
// * WebSocket configuration to enable WebSocket support in Spring Boot
// */
//@Slf4j
//@Configuration
//@EnableWebSocketMessageBroker
//@RequiredArgsConstructor
//public class WebSocketConfig implements WebSocketMessageBrokerConfigurer, WebSocketConfigurer {
//
//    private final WebSocketAuthInterceptor webSocketAuthInterceptor;
//    private final SpringWebSocketHandler springWebSocketHandler;
//
//    @Value("${server.servlet.context-path:}")
//    private String contextPath;
//
//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        // 客户端订阅消息的前缀
//        registry.enableSimpleBroker("/topic", "/queue", "/user");
//
//        // 客户端发送消息的前缀
//        registry.setApplicationDestinationPrefixes("/app");
//
//        // 指定用户消息的前缀，默认为/user/
//        registry.setUserDestinationPrefix("/user/");
//
//        log.info("MessageBroker configured with application prefix: /app");
//    }
//
//    @Override
//    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        // WebSocket连接端点，允许跨域
//        registry.addEndpoint("/ws")
//                .setAllowedOriginPatterns("*")
//                .withSockJS();
//
//        // Register another endpoint for the moments websocket
//        registry.addEndpoint("/moment/ws")
//                .setAllowedOriginPatterns("*")
//                .withSockJS();
//
//        // Add plain WebSocket endpoint for moments without SockJS
//        registry.addEndpoint("/moment/ws")
//                .setAllowedOriginPatterns("*");
//
//        // Register test endpoint
//        registry.addEndpoint("/test/ws")
//                .setAllowedOriginPatterns("*")
//                .withSockJS();
//
//        log.info("STOMP endpoints registered with context path: {}", contextPath);
//    }
//
//    @Override
//    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        // Register the Spring WebSocket handler for the plain WebSocket endpoint
//        registry.addHandler(springWebSocketHandler, "/spring-ws")
//                .setAllowedOrigins("*");
//
//        log.info("Registered Spring WebSocket handler at /spring-ws");
//    }
//
//    @Override
//    public void configureClientInboundChannel(ChannelRegistration registration) {
//        // 添加认证拦截器
//        registration.interceptors(webSocketAuthInterceptor);
//        log.info("Client inbound channel configured with auth interceptor");
//    }
//
//    /**
//     * 开启WebSocket支持
//     * 这个Bean会自动注册带有@ServerEndpoint注解的Bean
//     * @return ServerEndpointExporter实例
//     */
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter() {
//        log.info("Registering ServerEndpointExporter for @ServerEndpoint annotations");
//        return new ServerEndpointExporter();
//    }
//
//    /**
//     * 配置ObjectMapper用于JSON处理
//     * @return 配置好的ObjectMapper实例
//     */
//    @Bean
//    public ObjectMapper objectMapper() {
//        ObjectMapper mapper = new ObjectMapper();
//
//        // 设置日期格式
//        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//
//        // 忽略未知属性
//        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//
//        // 忽略空值
//        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//
//        // 格式化输出
//        mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
//
//        // 添加Java 8日期时间模块
//        mapper.registerModule(new JavaTimeModule());
//        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//
//        log.info("Configured custom ObjectMapper for JSON processing");
//        return mapper;
//    }
//}